package io.github.wx.service;

import com.jfinal.kit.JMap;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import io.github.wx.common.constant.EnumFuctionStatus;
import io.github.wx.common.easyui.DataGrid;
import io.github.wx.dao.model.SysRole;
import io.github.wx.dao.model.SysUser;

import java.util.List;

/**
 * Created by jie on 2017/4/8.
 * 角色管理
 */
public class RoleService {
    /**
     * 分页查询角色信息
     *
     * @param page page
     * @param rows page
     * @return DataGrid
     */
    public DataGrid<SysRole> getRolePage(Integer page, Integer rows) {
        String select = "select * ";
        String sqlExcepteSelect = "from sys_role where del_flag = ? ";
        Page<SysRole> userPage = SysRole.dao.paginate(page, rows, select, sqlExcepteSelect, EnumFuctionStatus.NORMAL_STATUS.getCode());
        return new DataGrid<SysRole>(userPage);
    }

    /**
     * 获取用户的角色
     *
     * @param loginName 登录名
     * @return SysRole
     */
    public SysRole findUserRole(String loginName) {
        JMap params = new JMap();
        params.put("username", loginName);
        params.put("del_flag", EnumFuctionStatus.NORMAL_STATUS.getCode());
        SqlPara sqlPara = SysUser.dao.getSqlPara("role.findUserRole", params);
        return SysRole.dao.findFirst(sqlPara);
    }

    /**
     * 获取角色的id-value对
     *
     * @return List
     */
    public List<SysRole> roleCombo() {
        JMap params = new JMap();
        params.put("del_flag", EnumFuctionStatus.NORMAL_STATUS.getCode());
        SqlPara sqlPara = SysUser.dao.getSqlPara("role.findRoleCombo", params);
        return SysRole.dao.find(sqlPara);
    }
}
